# This file contains the fastlane.tools configuration
# You can find the documentation at https://docs.fastlane.tools
#
# For a list of all available actions, check out
#
#     https://docs.fastlane.tools/actions
#
# For a list of all available plugins, check out
#
#     https://docs.fastlane.tools/plugins/available-plugins
#

# Uncomment the line if you want fastlane to automatically update itself
# update_fastlane

default_platform(:android)

platform :android do

  before_all do |lane, options|
    FileUtils.cp("../app/src/main/assets/tangem-app-config/android/google-services.json", "../app")
  end

  desc "Runs all the tests"
  lane :test do
    gradle(task: "detekt")
    gradle(task: "testDebugUnitTest")
  end

  desc "Build a signed release APK"
  lane :release do |options|
     gradle(
       task: "clean assemble",
       build_type: "Release",
       properties: {
         "android.injected.signing.store.file" => options[:keystore],
         "android.injected.signing.store.password" => options[:store_password],
         "android.injected.signing.key.alias" => options[:key_alias],
         "android.injected.signing.key.password" => options[:key_password],
       })
  end

  desc "Build external and release APKs"
  lane :build do |options|
     gradle(
       task: "clean assemble",
       build_type: "External",
       properties: {
           'versionCode' => options[:versionCode],
           'versionName' => options[:versionName],
         })
     gradle(
       task: "bundle",
       build_type: "Release",
       properties: {
          'versionCode' => options[:versionCode],
          'versionName' => options[:versionName],
          "android.injected.signing.store.file" => options[:keystore],
          "android.injected.signing.store.password" => options[:store_password],
          "android.injected.signing.key.alias" => options[:key_alias],
          "android.injected.signing.key.password" => options[:key_password],
       }
     )
     gradle(
       task: "assemble",
       build_type: "Release",
       properties: {
         'versionCode' => options[:versionCode],
         'versionName' => options[:versionName],
         "android.injected.signing.store.file" => options[:keystore],
         "android.injected.signing.store.password" => options[:store_password],
         "android.injected.signing.key.alias" => options[:key_alias],
         "android.injected.signing.key.password" => options[:key_password],
       }
     )
  end

  desc "Submit a new Beta Build to Firebase App Distribution"
  lane :beta do |options|
       firebase_app_distribution(
            app: options[:app_id],
            apk_path: options[:apk_path],
            groups: options[:groups])
  end

  desc "Publish internal and external builds to Firebase App Distribution"
  lane :publishToFirebase do |options|
    gradle(
      task: "clean assemble",
      build_type: "Internal",
      properties: {
        'versionCode' => ENV['versionCode'],
        'versionName' => ENV['versionName'],
      }
    )
    firebase_app_distribution(
      app: ENV['app_id_internal'],
      apk_path: ENV['apk_path_internal'],
      groups: ENV['groups'],
      release_notes: ENV['releaseNotes']
    )
  end
end
